package org.aceor.mddal.common.utils;

import com.google.common.base.Joiner;
import com.google.common.collect.Lists;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

/**
 * Created by lxue on 16/3/24.
 */
public class CommonUtil {
    public static void forEachPrint(Collection<?> collection) {
        int index = 1;
        for (Object object : collection) {
            if (object instanceof ResultSet) {
                print("Result" + index++ + ":");
                ResultSet resultSet = (ResultSet) object;
                printResultSet(resultSet);
            } else {
                System.out.println(object);
            }
        }
    }

    public static void printResultSet(ResultSet resultSet) {
        try {
            while (resultSet.next()) {
                int columnCount = resultSet.getMetaData().getColumnCount();
                List<Object> columns = Lists.newLinkedList();
                for (int i = 1; i <= columnCount; i++) {
                    Object column = resultSet.getObject(i);
                    columns.add(column == null ? "NULL" : column);
                }
                print(Joiner.on(",").join(columns));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void print(Class<?> clazz, Object object) {
        System.out.println(clazz + "----" + object);
    }

    public static void print(Object object) {
        System.out.println(object);
    }
}
